//
//  LKShopInformationViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/5/28.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKShopInformationViewController.h"
#import <UFKit/UFKit.h>
#import "LKUFCustomRow.h"
#import "LKUFCustomRowCell.h"
#import "LKUFCustomButtonCell.h"
#import "LKUFCustomRow.h"
#import "LKUFCustomBottomRow.h"
#import "LKUserInforTool.h"
#import "LKMyShopInformationModel.h"
#import "LKOpenServiceViewController.h"
@interface LKShopInformationViewController ()


@property(nonatomic,strong)UFFormView *inforFormview;

@property(nonatomic,strong)LKMyShopInformationModel *inforModel;

@property(nonatomic,assign)BOOL isOpen;
@property(nonatomic,copy)NSString *startTime;
@property(nonatomic,copy)NSString *endTime;
@end

@implementation LKShopInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商户信息";
    self.isOpen = YES;
    [self setNavgationRightBtn];
    [self getShopDetailInformation];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(relo:) name:@"reloadShopInformation" object:nil];

    // Do any additional setup after loading the view.
}

-(void)relo:(NSNotification *)infor{
    [self getShopDetailInformation];
}
/**
 设置返回按钮样式
 */
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"保存" titleColor:COLOR_HEX(0xF86747) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 12] backgroudColor:[UIColor whiteColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        NSLog(@"%@",[weakSelf.inforFormview toDictionary]);
        NSDictionary *param = [weakSelf.inforFormview toDictionary];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopInforEditUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopDetail" object:nil];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    rightBtn.layer.borderColor = COLOR_HEX(0xF86747).CGColor;
    rightBtn.layer.borderWidth = 1;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
//创建表单样式
-(void)createInformView{
    WS(weakSelf)
    self.inforFormview = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
       make
        .registerRow([LKUFCustomRow class], [LKUFCustomButtonCell class])
        .registerRow([LKUFCustomBottomRow class], [LKUFCustomRowCell class])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .headerStyle([UFSectionStyle makeSectionStyle:^(UFSectionStyleMaker * _Nonnull make) {
                make
                .height(10);
            }])
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);

            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"商户ID")
                .name(@"mch_uid")
                .value(self.inforModel.mch_id);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"所属行业")
                .name(@"industry")
                .value(self.inforModel.industry);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"推荐人")
                .name(@"推荐人")
                .value(self.inforModel.referrer);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"商户手机号")
                .name(@"merchant_phone")
                .value(self.inforModel.merchant_phone);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .maxLength(20)
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"商户名称")
                .name(@"merchant_name")
                .value(self.inforModel.merchant_name);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .maxLength(12)
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"商户简称")
                .name(@"merchant_jiancheng")
                .value(self.inforModel.merchant_jiancheng);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"店铺类型")
                .name(@"merchant_type")
                .value(self.inforModel.merchant_type);
            }]);
        }])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make

            .headerStyle([UFSectionStyle makeSectionStyle:^(UFSectionStyleMaker * _Nonnull make) {
                 make
                 .height(10);
             }])
             .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
                make
                 .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                 .color(labelBlackColor)
                 .textAlignment(NSTextAlignmentLeft);
             }])
             .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                 make
                 .textAlignment(NSTextAlignmentRight)
                 .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                 .color(labelBlackColor);
             }])
            .addRow([LKUFCustomRow makeRow:^(UFRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"小程序行业")
                .name(@"小程序行业")
                .ext(@"我是自定义Row");
            }])
            .addRow([UFAreaPickerRow makeAreaPickerRow:^(UFAreaPickerRowMaker * _Nonnull make) {
                make
                .areaSeparator(@",")
                .height(RowHeightDefault)
                .title(@"店铺地址")
                .name(@"address")
                .value([NSString stringWithFormat:@"%@%@%@%@",self.inforModel.province,self.inforModel.city,self.inforModel.county,self.inforModel.address])
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"邮箱")
                .name(@"e_mail")
                .value(self.inforModel.e_mail);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .height(RowHeightDefault)
                .title(@"客服电话")
                .name(@"kefu_phone")
                .value(self.inforModel.kefu_phone);
            }])
            .addRowWithBlock(^__kindof UFRow * _Nonnull{
                LKUFCustomBottomRow *row = [[LKUFCustomBottomRow alloc] init];
                row.showRight = weakSelf.showRight;
                row.showLeft = weakSelf.showLeft;
                row.height = RowHeightDefault;
                row.title = @"开通服务";
                row.rowDidSelected = ^(__kindof UFRow * _Nonnull row) {
                    LKOpenServiceViewController *openserviccVc = [[LKOpenServiceViewController alloc] init];
                    openserviccVc.mid = weakSelf.inforModel.mch_id;
                    openserviccVc.startTime = weakSelf.startTime;
                    openserviccVc.endTime = weakSelf.endTime;
                    [weakSelf.navigationController pushViewController:openserviccVc animated:YES];
                };
                return row;
            });

        }])
        .addToSuperView(self.view);
    }];
    [self.inforFormview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(self.view);
    }];
}

/// 获取商户的详细信息
-(void)getShopDetailInformation{
    WS(weakSelf)
    [SVProgressHUD show];
    NSDictionary *param = @{@"mch_id":self.shoplistModel.mch_id,@"merchant_phone":self.shoplistModel.merchant_phone};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyshopDetaiInforUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            self.inforModel = [LKMyShopInformationModel modelWithDictionary:responseObject[@"data"]];
            NSDictionary *param = @{@"is_view":@"1",@"mch_id":self.inforModel.mch_id};
             [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopYouhuiquan header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                NSString *containStr = responseObject[@"data"][@"agency_business"];
                self.startTime = responseObject[@"data"][@"valid_coupon"];
                self.endTime = responseObject[@"data"][@"valid_two"];
                if (containStr.length == 0) {
                    weakSelf.showRight = NO;
                    weakSelf.showLeft = NO;
                }else if([containStr containsString:@"3"]){
                    weakSelf.showRight = YES;
                    weakSelf.showLeft = YES;
                }else if ([containStr containsString:@"1"] && ![containStr containsString:@"3"]){
                    weakSelf.showLeft = YES;
                    weakSelf.showRight = NO;
                }
                [self createInformView];
                [self.inforFormview reloadData];
            } failure:^(NSString * _Nullable errorMessage) {
                
            }];
 
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
//-(void)getOpenService{
//    NSDictionary *param = @{@"is_view":@"0",@"agency_business":@"valid_coupon",@"valid_two":@""};
//    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopYouhuiquan header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
//
//    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
//
//    } failure:^(NSString * _Nullable errorMessage) {
//
//    }];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
